﻿using BUS;
using DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
namespace MyWeb.MasterPage
{
    public partial class _FrontEnd_Index : System.Web.UI.MasterPage
    {
        NguoiDungDTO nddto = null;
        MenuBUS mnb = new BUS.MenuBUS();
        protected void Page_Load(object sender, EventArgs e)
        {
            GetMainMenu();
            LoadCart();
            if (!Page.IsPostBack)
            {
                nddto = (NguoiDungDTO)Session["KhachHang"];
                if (Session["KhachHang"] != null)
                {
                    alreadylogin.Visible = true;
                    notlogin.Visible = false;
                    lnktenuser.Text = nddto.Hoten.ToString();
                }
                else
                {
                    alreadylogin.Visible = false;
                    notlogin.Visible = true;
                }
            }
        }
        /// <summary>
        /// Load menu from csdl
        /// </summary>
        private void GetMainMenu()
        {
            active.Controls.Clear();
            active.Items.Clear();
            DataTable dt = new DataTable();
            dt = mnb.SelectMenu();
            foreach (DataRow row in dt.Rows)
            {
                MenuItem item = new MenuItem();
                item.Text = row["name"].ToString();
                item.NavigateUrl = "~/GUI/User/" + row["link"].ToString()+".aspx";              
                active.Items.Add(item);
            }
        }
        protected void lnklogout_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("TrangChu.aspx");
        }

        protected void rpGioHang_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.Equals("remove"))
            {
                int masp = Convert.ToInt32(e.CommandArgument);
                ShoppingCart.Instance.RemoveItem(masp);
            }
            LoadCart();
        }

        private void LoadCart()
        {
            if (ShoppingCart.Instance.Items.Count > 0)
            {
                rpGioHang.DataSource = ShoppingCart.Instance.Items;
                rpGioHang.DataBind();
            }
            else
            {
                cartvisible.Visible = false;
            }
        }
    }
}